﻿using FastORM.Attribute;
using FastORM.Entity;
using FastORM.Mapping;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Base.Desgin
{
    public class BaseDBML<TEntity> : BaseML<TEntity> where TEntity : BaseEntity
    {
        public string TableName { set; get; }
        public BaseDBML()
        {
            Type type = typeof(TEntity);
            TableName = StructMapping.GetInstance().GetMapperEntity(type).TableName;
        }

        public virtual Page SelectPage(string StrColumn, string StrWhere, string StrOrder, List<ParamEntity> ParamList = null)
        {
            List<TEntity> list = DTO.SelectPage<TEntity>(StrColumn, StrWhere, StrOrder, ParamList);
            int count = DTO.SelectCount(TableName, StrWhere);
            Page page = new Page();
            page.Rows = list;
            page.Total = count;
            return page;
        }

        public virtual List<TEntity> SelectPageList(string StrColumn, string StrWhere, string StrOrder, List<ParamEntity> ParamList = null)
        {
            return DTO.SelectPage<TEntity>(StrColumn, StrWhere, StrOrder, ParamList);
        }

        public virtual List<TEntity> SelectPageList(string StrColumn, string StrWhere, string StrOrder, int PageIndex, int PageSize, List<ParamEntity> ParamList = null)
        {
            return DTO.SelectPage<TEntity>(StrColumn, StrWhere, StrOrder, PageIndex, PageSize, ParamList);
        }

        public virtual List<TEntity> SelectList(string StrColumn, string StrWhere, string StrOrder, List<ParamEntity> ParamList = null)
        {
            return DTO.Select<TEntity>(TableName, StrColumn, StrWhere, StrOrder, ParamList);
        }

        public virtual List<TEntity> SelectAllList(string Column = "*",  string StrColumn = "1=1", List<ParamEntity> ParamList = null, string StrOrder = "SortNum desc")
        {
            return DTO.Select<TEntity>(TableName, Column, StrColumn, StrOrder, ParamList);
        }

        public virtual DataTable SelectTablePageList(string StrColumn, string StrWhere, string StrOrder, List<ParamEntity> ParamList = null)
        {
            return DTO.SelectPage(TableName, StrColumn, StrWhere, StrOrder, ParamList);
        }

        public virtual DataTable SelectTablePageList(string StrColumn, string StrWhere, string StrOrder, int PageIndex, int PageSize, List<ParamEntity> ParamList = null)
        {
            return DTO.SelectPage(TableName, StrColumn, StrWhere, StrOrder, PageIndex, PageSize, ParamList);
        }

        public virtual DataTable SelectTableList(string StrColumn, string StrWhere, string StrOrder, List<ParamEntity> ParamList = null)
        {
            return DTO.Select(TableName, StrColumn, StrWhere, StrOrder, ParamList);
        }

        public virtual TEntity SelectOne(string StrColumn, string StrWhere, List<ParamEntity> ParamList = null)
        {
            List<TEntity> EntityList = SelectList(StrColumn, StrWhere, "", ParamList);
            if (EntityList.Count>0)
            {
                return EntityList[0];
            }
            else
            {
                return null;
            }
        }

        public virtual TEntity SelectOne(string StrColumn, string FieldName,string FieldVlaue)
        {
            List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = FieldName, ParamValue = FieldVlaue } };
            return SelectOne(StrColumn, string.Format("{0}=@{0}", FieldName), ParamList);
        }

        public virtual DataRow SelectRowOne(string StrColumn, string StrWhere, List<ParamEntity> ParamList = null)
        {
            DataTable EntityList = SelectTableList(StrColumn, StrWhere, "", ParamList);
            if (EntityList.Rows.Count > 0)
            {
                return EntityList.Rows[0];
            }
            else
            {
                return null;
            }
        }

        public virtual DataRow SelectRowOne(string StrColumn, string FieldName, string FieldVlaue)
        {
            List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = FieldName, ParamValue = FieldVlaue } };
            return SelectRowOne(StrColumn, string.Format("{0}=@{0}", FieldName), ParamList);
        }

        public virtual int SelectCount(string StrWhere, List<ParamEntity> ParamList = null)
        {
            return DTO.SelectCount(TableName, StrWhere, ParamList);
        }
    }
}
